package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import business.BusinessRecommendation;
import business.BusinessSeries;
import domain.Recommendation;
import domain.User;

/**
 * Servlet implementation class EventController
 */
@WebServlet("/EventController")
public class RecomendationController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RecomendationController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		newRecommendation(request, response);
	}

	/**
//	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
//	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		newRecommendation(request, response);
	}


	private void newRecommendation(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		
		
	String comment  =	request.getParameter("Descripcion");
	Integer idVideoRec = new Integer(request.getParameter("idVideo"));
	String [] friends = request.getParameterValues("friends");
	
	  HttpSession session = request.getSession();
	    User sessionUser = (User) session.getAttribute("session.user");
	    
	    Integer idUser = sessionUser.getIdUser();
	    
	  for(int i=0; i<friends.length;i++){
		  Recommendation recomendation =  new Recommendation(idVideoRec,idUser, new Integer(friends[i]), comment, null);
		  BusinessRecommendation.createRecomendation(recomendation);
	  }
	  if(BusinessSeries.getSeriesByIdVideo(idVideoRec)==null)
	  {
		  RequestDispatcher redirect = request.getRequestDispatcher("movie.jsp");
	  	  redirect.forward(request, response);
	  }
	  else
	  {
		  RequestDispatcher redirect = request.getRequestDispatcher("serie.jsp");
	  	  redirect.forward(request, response);
	  }
	
	
	}
	

}
